package com.chinasofti.project.service.product.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinasofti.project.common.pageQuery.PageResult;
import com.chinasofti.project.dal.mapper.custom.BzBookProductMapperCustom;
import com.chinasofti.project.dal.mapper.custom.BzFavoriteMapperCustom;
import com.chinasofti.project.dal.model.BzFavorite;
import com.chinasofti.project.dal.model.BzFavoriteExample;
import com.chinasofti.project.dal.model.custom.BzProduct;
import com.chinasofti.project.service.product.ProductService;
import com.chinasofti.project.web.info.FavoriteQueryInfo;

@Service("ProductService")
public class ProductServiceImpl implements ProductService {

	@Autowired
	private BzFavoriteMapperCustom bzFavoriteMapperCustom;

	@Autowired
	private BzBookProductMapperCustom bzBookProductMapperCustom;

	@Override
	public List<BzProduct> theFirstImg(String productThreeType) {
		List<BzProduct> list = bzBookProductMapperCustom.selectByProductThreeType6(productThreeType);
		return list;
	}

	@Override
	public List<BzProduct> otherImg(String productThreeType) {
		List<BzProduct> list = bzBookProductMapperCustom.selectByProductThreeType8(productThreeType);
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.chinasofti.project.service.product.ProductService#addFavorite(int,
	 * int)
	 */
	@Override
	public int addFavorite(int userId, int productId) {
		BzFavorite record = new BzFavorite();
		record.setProductId(productId);
		record.setUserId(userId);
		return bzFavoriteMapperCustom.insert(record);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.chinasofti.project.service.product.ProductService#
	 * selecteFavoriteByUserId(int)
	 */
	@Override
	public PageResult<Map> selecteFavoriteByUserId(int userId, int pageIdx) {
		BzFavoriteExample bzFavoriteExample = new BzFavoriteExample();
		bzFavoriteExample.createCriteria().andUserIdEqualTo(userId);
		long totalCount = bzFavoriteMapperCustom.countByExample(bzFavoriteExample);
		FavoriteQueryInfo favoriteQueryInfo = new FavoriteQueryInfo(pageIdx == 0 ? 1 : pageIdx);
		favoriteQueryInfo.setUserId(userId);
		List<BzFavorite> Favorites = bzFavoriteMapperCustom.selectFavoriteByUserId(favoriteQueryInfo);
		List<Map> resultList = new ArrayList<>();
		for (BzFavorite bf : Favorites) {
			BzProduct bp = bzBookProductMapperCustom.selectByProductId(bf.getProductId());
			Map favoriteMap = new HashMap();
			favoriteMap.put("favoriteId", bf.getFavoriteId());
			favoriteMap.put("productName", bp.getProductName());
			favoriteMap.put("productPrice", bp.getProductPrice());
			favoriteMap.put("bookMainImg", bp.getBookMainImg());
			resultList.add(favoriteMap);
		}
		return new PageResult<>(totalCount, favoriteQueryInfo.getPageSize(), resultList);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.chinasofti.project.service.product.ProductService#deleteFavorite(int)
	 */
	@Override
	public int deleteFavorite(int favoriteId) {
		return bzFavoriteMapperCustom.deleteByPrimaryKey(favoriteId);
	}

}
